﻿using DiagnosticExplorer;

namespace Streambolics.App.Gpl
{
    public class DiagExLogWriter : ComputingModule
    {
        private EventSink _Sink;

        protected override void DoStart ()
        {
            _Sink = EventSink.GetSink ("Global Log", "System");
            GlobalLog.EventLogged += new GlobalLogEventHandler (GlobalLog_EventLogged);
            DeclareStarted ();
        }

        protected override void DoStop ()
        {
            GlobalLog.EventLogged -= new GlobalLogEventHandler (GlobalLog_EventLogged);
            DeclareStopped ();
        }

        void GlobalLog_EventLogged (object sender, GlobalLogEventArgs e)
        {
            if (e.Exception != null)
            {
                _Sink.LogEvent (EventSeverity.High, e.Message);
            }
            else
                switch (e.Severity)
                {
                    case GlobalLogSeverity.Error:
                    case GlobalLogSeverity.Fatal:
                        {
                            _Sink.LogEvent (EventSeverity.High, e.Message);
                            break;
                        }

                    case GlobalLogSeverity.Warning:
                        {
                            _Sink.LogEvent (EventSeverity.Medium, e.Message);
                            break;
                        }
                    default:
                        {
                            _Sink.LogEvent (EventSeverity.Low, e.Message);
                            break;
                        }
                }
        }

    }
}
